package sitar;

import flash.display.StageScaleMode;
import flash.events.ContextMenuEvent;
import flash.Lib;
import flash.filesystem.File;
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.net.FileFilter;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import oboe.controllers.Controller;
import oboe.core.ISigSlot;
import oboe.core.Oboe;
import oboe.core.SigSlot;
import oboe.util.ISigSlotHelper;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.InvokeEvent;

/**
 * ...
 * @author Felipe Manga
 */

 using oboe.util.ISigSlotHelper;
 using oboe.util.Prototype;

class Main extends Sprite, implements ISigSlot
{
	var sigslot:SigSlot;
	var txt:TextField;
	
	static var menuOptions =
		[
			{newProject : "New project..." },
			{generate : "Generate Source..." },
			{exit : "Exit"}
		];
	
	private function new()
	{
		super();
		this.init( new Controller(this) );
		
		
		this.txt = new TextField();
		this.txt.x = this.txt.y = 0;
		this.txt.width = Lib.current.stage.stageWidth;
		this.txt.height = Lib.current.stage.stageHeight;
		this.txt.multiline = true;
		this.txt.selectable = false;
		this.txt.type = TextFieldType.DYNAMIC;
		this.addChild( this.txt );
		
		Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
		Lib.current.stage.addEventListener( Event.RESIZE, onResize );
		
		this.contextMenu = new ContextMenu();
		this.contextMenu.hideBuiltInItems();
		var THIS = this;
		for ( d in menuOptions )
		{
			for ( f in Reflect.fields( d ) )
			{
				var item = new ContextMenuItem( Reflect.field(d, f) );
				item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):Void { THIS.message(f,[]); } );
				this.contextMenu.customItems.push( item );
			}
		}
		
		this.sigslot.addChild(new NewProjectWizard( File.desktopDirectory.resolvePath("tmp") ));
	}
	
	@slot function newProject():Void
	{
		var dir = flash.filesystem.File.desktopDirectory;
		dir.browseForDirectory("Project Folder");
		var THIS = this;
		dir.addEventListener( Event.SELECT, function(e:Event):Void { THIS.sigslot.addChild(new NewProjectWizard( e.target )); } );
	}
	
	@slot function newProjectDone( root:File ):Void
	{
		this.message("log", ["Complete."]);
		this.sigslot.addChild(new Generator( root ));
	}
	
	@slot function generate():Void
	{
		var dir = flash.filesystem.File.desktopDirectory;
		dir.browseForDirectory("Project Folder");
		var THIS = this;
		dir.addEventListener( Event.SELECT, function(e:Event):Void { THIS.sigslot.addChild(new Generator( e.target )); } );
	}
	
	@slot function exit():Void
	{
		NativeApplication.nativeApplication.exit();
	}
	
	@slot @vararg function log( args:Array<Dynamic> ):Void
	{
		var buf:String = "";
		for ( i in args ) buf += i + " ";
		buf = buf.substr(0, buf.length - 1) + "\n";
		txt.text += buf;
	}

	@slot @vararg function error( args:Array<Dynamic> ):Void
	{
		var buf:String = "";
		for ( i in args ) buf += i + " ";
		buf = "ERROR: " + buf.substr(0, buf.length - 1) + "\n";
		txt.text += buf;
	}
	
	function onResize( e:Event ):Void
	{
		this.txt.width = Lib.current.stage.stageWidth;
		this.txt.height = Lib.current.stage.stageHeight;
	}
	
	static function main() 
	{
		var app = NativeApplication.nativeApplication;

		app.addEventListener(InvokeEvent.INVOKE, function(e : InvokeEvent) 
		{
			Oboe.initOboe(
				{
					TRACE_FPS : true,
					DEBUG : /* * / false /*/ true /* */
				},
				Main
			);			
		});
	}
}